package com.school.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.school.dao.HljtCheckWorkAttendanceDao;
import com.school.dao.HljtClassDao;
import com.school.dao.HljtStudentDao;
import com.school.entity.HljtClass;
import com.school.entity.HljtDevicetask;
import com.school.entity.HljtStudent;
import com.school.entity.HljtTranslate;
import com.school.service.HljtCheckWorkAttendanceService;
@Service
public class HljtHljtCheckWorkAttendanceServiceImpl  implements HljtCheckWorkAttendanceService{
	
	@Autowired
	private HljtCheckWorkAttendanceDao hljtCheckWorkAttendanceDao;
	@Autowired
	private HljtStudentDao hljtStudentDao;
	@Autowired
	private HljtClassDao hljtClassDao;
	
	@Override
	@Transactional(readOnly=true)
	public List<HljtStudent> cardStudent(HljtStudent hljtStudent) {
		 List<HljtStudent> student = hljtCheckWorkAttendanceDao.cardStudent(hljtStudent);
		List<HljtStudent> newList = new ArrayList<>();
	       if(null !=student) {
	    	 Map<String, Object> map = new HashMap<>();
	         for (HljtStudent cd:student) {
	        	 String key = cd.getStudentCardId();
	            if(map.containsKey(key)){}else {
	            	 map.put(key, cd);
	            	 newList.add(cd);
	            }
	        }
		}
	        return newList;  
	}
	@Override
	@Transactional(readOnly=true)
	public List<HljtStudent> notCardStudent(HljtStudent hljtStudent) {
	        return hljtCheckWorkAttendanceDao.notCardStudent(hljtStudent);
	}
	
	@Override
	@Transactional(rollbackFor=Exception.class)
	public Map<String ,Object> addCheckWorkAttendance(HljtTranslate hljtTranslate) throws Exception {
		Map<String ,Object> map = new HashMap<>();
		HljtClass hljtClass = hljtClassDao.findClass(hljtTranslate.getClassid());
		if (null == hljtClass) {
			map.put("classid", false);
		}else {
			map.put("classid", true);
			Date date = new Date();
			HljtStudent hljtStudent = new HljtStudent();
			hljtStudent.setIccard(hljtTranslate.getIccardid());
			HljtStudent student = hljtStudentDao.studentByIccard(hljtStudent);
			if(null ==student) {
				//该卡没有绑定学生
				map.put("cardNotBoundStudent", false);
			}else {
				//该卡绑定学生
				map.put("cardNotBoundStudent", true);
				if(hljtTranslate.getAttendanceModel()==2) {
					List<HljtDevicetask> hljtDevicetask  = hljtCheckWorkAttendanceDao.devicetaskByPatternId(hljtTranslate);
						if(null!=hljtDevicetask&&hljtDevicetask.size()>0&&this.getTimeComparison(date, hljtDevicetask)) {
						//模式内打卡
						map.put("modeAttendance", true);
						Integer row = null;
						row = this.getPlayCardInModeAttendance(hljtTranslate, date);
						if(null==row) {
							//已打卡
							map.put("row", row);
						}else {
							//打卡成功
							student.setCardTime(date);
							map.put("row", row);
							map.put("student", student);
						}
					}else {
						//模式外打卡
						student.setCardTime(date);
						map.put("student", student);
						map.put("modeAttendance", false);
						this.getPlayCardOutModeAttendance(hljtTranslate, date);
					}
				}else {
					//模式外打卡
					student.setCardTime(date);
					map.put("student", student);
					map.put("modeAttendance", false);
					this.getPlayCardOutModeAttendance(hljtTranslate, date);
				}
			}
		}
		return map;
	}
	  // 一天的开始时间
	     private static Date getStartTime() {  
	         Calendar todayStart = Calendar.getInstance();  
	         todayStart.set(Calendar.HOUR_OF_DAY, 0);  
	         todayStart.set(Calendar.MINUTE, 0);  
	         todayStart.set(Calendar.SECOND, 0);  
	         todayStart.set(Calendar.MILLISECOND, 0);  
	         return todayStart.getTime();  
	     }
		@Override
		public Integer getPlayCardInModeAttendance(HljtTranslate hljtTranslate , Date date) {
			HljtTranslate h = new HljtTranslate();
			h.setIccardid(hljtTranslate.getIccardid());
			h.setLatetime(HljtHljtCheckWorkAttendanceServiceImpl.getStartTime());
			Integer id = hljtCheckWorkAttendanceDao.translateByICCardIdLateTime(h);
			Integer row = null;
			if(null == id) {
				//没有打卡
				hljtTranslate.setLatetime(date);
				row = hljtCheckWorkAttendanceDao.addCheckWorkAttendance(hljtTranslate);
				return row;
			}else {
				//已打卡
				return row;
			}
		}
		@Override
		public Integer getPlayCardOutModeAttendance(HljtTranslate hljtTranslate , Date date) {
			Integer row = null;
			hljtTranslate.setLatetime(date);
			row = hljtCheckWorkAttendanceDao.addCheckWorkAttendance(hljtTranslate);
			return row;
		}  
		
		
		protected boolean getTimeComparison(Date date ,List<HljtDevicetask> dev ) {
			SimpleDateFormat s = new SimpleDateFormat("HH:mm");
			String stringDate= s.format(date);
			String replaceDate = stringDate.replaceAll(":","");
			int intDate = Integer.parseInt(replaceDate);
			int intStartTime = 0;
			int intEndTime = 0;
			if(null !=dev && dev.size()>0) {
				for (HljtDevicetask hljtDevicetask : dev) {
					String replaceStartTime = hljtDevicetask.getStarttime().replaceAll(":","");
					intStartTime = Integer.parseInt(replaceStartTime);
					String replaceEndTime = hljtDevicetask.getEndtime().replaceAll(":","");
					intEndTime = Integer.parseInt(replaceEndTime);
					if(intDate>intStartTime && intDate<intEndTime) {
						return true;
					}
				}
			}
			return false;
		}
}
